home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 440_01 / !best004.c < prev    next >
C/C++ Source or Header  |  1994-04-17  |  17KB  |  412 lines

  1. /*==========================================================================
  2.  *
  3.  *  !BEST004.C                                        Sunday, April 17, 1994
  4.  *
  5.  *  .INI file routines
  6.  *  supplementary source file 4 for The BESTLibrary
  7.  *
  8.  *  Authored independently by George Vanous
  9.  *
  10.  *==========================================================================*/
  11.  
  12.  
  13. /* ------------------------------------------------------------------------ */
  14. /* ----------------------------  INCLUDE FILES  --------------------------- */
  15.  
  16. #include <dir.h>
  17. #include <dos.h>
  18. #include <alloc.h>
  19. #include <stdlib.h>
  20. #include "!bestlib.h"                  // include !BESTLIB.H in compilation
  21.  
  22. /* ------------------------------------------------------------------------ */
  23. /* ------------------------------  CONSTANTS  ----------------------------- */
  24.  
  25. /* used in procedure "ini_get_error" */
  26. #define BOOLEAN  0
  27. #define COLOR    1
  28. #define NUMBER   2
  29. #define POSITION 3
  30. #define ROWS     4
  31. #define STATE    5
  32. #define STRING   6
  33.  
  34. /* ------------------------------------------------------------------------ */
  35. /* ---------------------------  ERROR MESSAGES  --------------------------- */
  36.  
  37. #define ERR03 "\nI cannot open the initialization file -- I need this file to continue.\n\n"
  38. #define ERR04 "\nIt seems the initialization file is incorrect\n\nI do not know this color:  %s\n\nThis color is located here:\n%s\n\nThe colors I know are:  BLACK  BLUE  GREEN  CYAN  RED  MAGENTA  BROWN  LIGHTGREYDARKGREY  LIGHTBLUE  LIGHTGREEN  LIGHTCYAN  LIGHTRED  LIGHTMAGENT  YELLOW  WHITE\n"
  39. #define ERR05 "\nI believe the initialization file is incorrect\n\nThere is an invalid text string located here:\n%s\n\nThe offensive string is:  %s\n\nA valid string must be enclosed within quotation marks:  \"This string is OK!\"\n\nPlease remedy this problem.\n\n"
  40. #define ERR06 "\nI have found something incorrect in the initialization file\n\nThere is a section header missing from section:  %s\n\nI can not find this header:  %s\n\nPlease type it in.\n\n"
  41. #define ERR07 "\nThere is a problem with the initialization file\n\nI have located an open section\n\nI can not find this section terminator:  %s\n\nPlease input it.\n\n"
  42. #define ERR08 "\nI think something needs to be added to the initialization file\n\nI am missing an important option from this section:  %s\n\nCan you please insert this option:  %s\n\n"
  43. #define ERR09 "\nI cannot seem to understand one part of the initialization file\n\nI think an equality symbol, =, is missing from this section:\n%s\n\nI can not locate the = symbol for this option:  %s\n\nCan you please input it for me?\n\n"
  44. #define ERR10 "\nMaybe you can help me understand the initialization file\n\nThe problem lies in this section:\n%s\n\nThe parameter I am having trouble with is:  %s\n\nI think it can only be TRUE or FALSE -- can you please correct this problem?\n\n"
  45. #define ERR11 "\nThere seems to be an invalid number in the initialization file\n\nThe offensive number lies in this section:\n%s\n\nThe number I do not recognize is:  %s\n\nPlease try to correct this problem.\n\n"
  46. #define ERR12 "\nI am having difficulty understanding the initialization file\n\nMy question comes from this section:\n%s\n\nI do not know what is meant by this parameter:  %s\n\nI expected it to be either ON, OFF, or UNMODIFY -- can you help me?\n\n"
  47. #define ERR13 "\nThe initialization file must be in error\n\nI do not understand this screen location: %s\n\nIt is located in this section:\n%s\n\nI only understand these two screen positions:  TOP  BOTTOM\n\nPlease correct this problem and try again.\n\n"
  48. #define ERR14 "\nI think there is something that needs correcting in the initialization file\n\nI can only display 25 or 50 rows per screen, but in the section:\n%s\n\nI am asked to display %s rows.\n\nPlease input one of these values:  25, 50, or UNMODIFY.  Thank you.\n\n"
  49. #define ERR15 "\nI cannot find the file %s -- it was requested in the initialization file\n\nThis is the section that asked for the file:\n%s\n\nEither this file is not in the current directory or it is misspelled.\n\n"
  50.  
  51. /* ------------------------------------------------------------------------ */
  52. /* -------------------------  FUNCTION PROTOTYPES  ------------------------ */
  53.  
  54. void ini_get_error(char *section, char *str, word *ptr, byte type);
  55. char *ini_verify_format(char *section, char *section_title,
  56.                         char *option, word *ptr);
  57.  
  58. /* ------------------------------------------------------------------------ */
  59.  
  60.  
  61. /*----------------------------------------------------------------------------
  62.  * Translate the boolean word "TRUE" or "FALSE" into boolean TRUE or FALSE.
  63.  *
  64.  * "section"       - section text
  65.  * "section_title" - section title to read from
  66.  * "option"        - option to read from
  67.  *
  68.  * RETURNS:
  69.  * = TRUE if found string "TRUE"
  70.  * = FALSE if found string "FALSE"
  71.  */
  72. boolean ini_get_boolean(char *section, char *section_title, char *option)
  73. {
  74.   shortint value;                      // return value
  75.   char *str;                           // parameter
  76.   word  ptr[2];                        // two generic offset pointers
  77.  
  78.   str = ini_verify_format(section, section_title, option, ptr);
  79.  
  80.   if ((value = con_str_to_bool(str)) == -1)
  81.     ini_get_error(section, str, ptr, BOOLEAN);
  82.  
  83.   return( (boolean)value );
  84. }
  85.  
  86. /*----------------------------------------------------------------------------
  87.  * Translate a color name into its numerical equivalent.
  88.  *
  89.  * "section"       - section text
  90.  * "section_title" - section title to read from
  91.  * "option"        - option to read from
  92.  *
  93.  * RETURNS:
  94.  * = numerical equivalent of color name
  95.  */
  96. byte ini_get_color(char *section, char *section_title, char *option)
  97. {
  98.   shortint value;                      // return value
  99.   char *str;                           // parameter
  100.   word  ptr[2];                        // two generic offset pointers
  101.  
  102.   str = ini_verify_format(section, section_title, option, ptr);
  103.  
  104.   if ((value = con_str_to_color(str)) == -1)
  105.     ini_get_error(section, str, ptr, COLOR);
  106.  
  107.   return( (byte)value );
  108. }
  109.  
  110. /*----------------------------------------------------------------------------
  111.  * Display the offending parameter in the initialization file and exit to DOS
  112.  *   with the appropriate ERRORLEVEL.
  113.  *
  114.  * "section" - section text
  115.  * "str"     - parameter in question
  116.  * "ptr"     - two offset pointers
  117.  * "type"    - type of error
  118.  */
  119. void ini_get_error(char *section, char *str, word *ptr, byte type)
  120. {
  121.   char error[1000];                    // ample space for error message
  122.  
  123.   // isolate the section containing the error
  124.   *(section+ptr[0] + scan_byte(section+ptr[0], LF, NULL, FORWARD)-1) = NULL;
  125.  
  126.   // display the correct error message
  127.   switch (type) {
  128.     case BOOLEAN  : sprintf(error, ERR10, section+ptr[1], str            ); break;
  129.     case COLOR    : sprintf(error, ERR04, str,            section+ptr[1] ); break;
  130.     case NUMBER   : sprintf(error, ERR11, section+ptr[1], str            ); break;
  131.     case POSITION : sprintf(error, ERR13, str,            section+ptr[1] ); break;
  132.     case ROWS     : sprintf(error, ERR14, section+ptr[1], str            ); break;
  133.     case STATE    : sprintf(error, ERR12, section+ptr[1], str            ); break;
  134.     case STRING   : sprintf(error, ERR05, section+ptr[1], str            );
  135.   }
  136.   fprintf(stderr, error);
  137.   beep(); exit(3);
  138. }
  139.  
  140. /*----------------------------------------------------------------------------
  141.  * Translate an ASCII representation of a number into its numerical equivalent.
  142.  *
  143.  * "section"       - section text
  144.  * "section_title" - section title to read from
  145.  * "option"        - option to read from
  146.  *
  147.  * RETURNS:
  148.  * = numerical representation of number string
  149.  */
  150. int ini_get_number(char *section, char *section_title, char *option)
  151. {
  152.   char *str;                           // parameter
  153.   word  ptr[2];                        // two generic offset pointers
  154.  
  155.   str = ini_verify_format(section, section_title, option, ptr);
  156.  
  157.   if (!is_number_dec(str))
  158.     ini_get_error(section, str, ptr, NUMBER);